/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MultipleMessagePanel.java
 *
 * Created on 24 Sep, 2011, 1:14:29 PM
 */

package net.sf.sbv.ui;

import net.sf.sbv.modals.Contact;
import net.sf.sbv.modals.Message;

/**
 *
 * @author Pratik
 */
public class MultipleMessagePanel extends javax.swing.JPanel {
    private Contact contact;

    /** Creates new form MultipleMessagePanel */
    public MultipleMessagePanel() {
        initComponents();

        jScrollPane1.getVerticalScrollBar().setUnitIncrement(30);
        clearWithoutUpdate();
    }

    public void setContact(Contact contact) {
        this.contact = contact;
        updateContactData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contactPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbMsgCount = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lbReceivedCount = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbSentCount = new javax.swing.JLabel();
        lbContactDetails = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        messagesPanel = new javax.swing.JPanel();

        contactPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("From:");

        jLabel2.setText("Message Count:");

        lbMsgCount.setText("0");

        jLabel3.setText("Received:");

        lbReceivedCount.setText("0");

        jLabel5.setText("Sent:");

        lbSentCount.setText("0");

        lbContactDetails.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbContactDetails.setText("None");

        javax.swing.GroupLayout contactPanelLayout = new javax.swing.GroupLayout(contactPanel);
        contactPanel.setLayout(contactPanelLayout);
        contactPanelLayout.setHorizontalGroup(
            contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contactPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(contactPanelLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbMsgCount)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbReceivedCount)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbSentCount))
                    .addGroup(contactPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbContactDetails, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)))
                .addContainerGap())
        );
        contactPanelLayout.setVerticalGroup(
            contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contactPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbContactDetails))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contactPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbMsgCount)
                    .addComponent(jLabel3)
                    .addComponent(lbReceivedCount)
                    .addComponent(jLabel5)
                    .addComponent(lbSentCount))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        messagesPanel.setLayout(new javax.swing.BoxLayout(messagesPanel, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane1.setViewportView(messagesPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(contactPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(contactPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contactPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbContactDetails;
    private javax.swing.JLabel lbMsgCount;
    private javax.swing.JLabel lbReceivedCount;
    private javax.swing.JLabel lbSentCount;
    private javax.swing.JPanel messagesPanel;
    // End of variables declaration//GEN-END:variables

    private void updateContactData() {
        clearWithoutUpdate();

        //populate the contact details
        lbContactDetails.setText(contact.toString());
        lbMsgCount.setText(getCountOfMessages(-1));

        int receiveCount = 0, sentCount = 0;

        //render the message using message panel
        for(Message message : contact.getMessages()) {
            if(message.getMessageType() == Message.RECEIVED) {
                receiveCount++;
            } else {
                sentCount++;
            }

            messagesPanel.add(new SingleMessagePanel(message));
        }

        //scroll the scrollbar to bottom
        jScrollPane1.getVerticalScrollBar().setValue(jScrollPane1.getVerticalScrollBar().getMaximum());

        //render receive and sent count
        lbReceivedCount.setText(receiveCount + "");
        lbSentCount.setText(sentCount + "");

        //update the UI to reflect the changes done.
        this.updateUI();
    }

    /**
     * This method will remove all the existing data.
     */
    public void clearPreviousData() {
        clearWithoutUpdate();
        this.updateUI();
    }

    private void clearWithoutUpdate() {
        messagesPanel.removeAll();

        lbContactDetails.setText("None");
        lbMsgCount.setText("0");
        lbReceivedCount.setText("0");
        lbSentCount.setText("0");
    }

    private String getCountOfMessages(int msgType) {
        int count = 0;
        if(msgType == Message.RECEIVED || msgType == Message.SENT) {
            for(Message msg : contact.getMessages()) {
                if(msg.getMessageType() == msgType) {
                    count++;
                }
            }
        } else {
            count = contact.getMessages().size();
        }

        return count + "";
    }

}
